package com.atguigu.gmall.all.controller;


import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import io.prometheus.client.Collector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品搜索页面
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list")
    public String list(SearchParam searchParam, Model model){
        //回显页面上的关键词
        model.addAttribute("searchParam",searchParam);

        //根据关键词等相关条件查询的返回值对象
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //品牌集合
        model.addAttribute("trademarkList",responseVo.getTrademarkList());
        //URLParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //平台属性
        model.addAttribute("attrsList",responseVo.getAttrsList());
        //排序回显
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);

        //结果集
        model.addAttribute("goodsList",responseVo.getGoodsList());
        //当前页
        model.addAttribute("pageNo",responseVo.getPageNo());
        //总页数
        model.addAttribute("totalPages",responseVo.getTotalPages());

        //选择品牌之后回显已选择结果
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);

        //选择平台属性之后回显已选择平台属性结果
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);

        return "list/index";
    }

    //选择平台属性之后回显已选择平台属性结果
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                //attrName  attrValue   attrId
                map.put("attrId",p[0]);
                map.put("attrName",p[2]);
                map.put("attrValue",p[1]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //选择品牌之后回显已选择结果
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //构建页面排序红底样式的回显 asc desc回显
    private Map buildOrderMap(SearchParam searchParam) {
        Map map = new HashMap();
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            map.put("type",o[0]);
            map.put("sort",o[1]);
        } else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    //构建URLParam
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();

        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)){
            sb.append("keyword=").append(searchParam.getKeyword());
        }

        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(searchParam.getTrademark());
        }

        String[] props = searchParam.getProps();
        if (props!= null && props.length > 0){
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return "/list?" + sb.toString();
    }
}
